package sample;

import common.Constant;
import spider.Spider;
import spider.Spider_MQ;
import com.alibaba.rocketmq.client.exception.MQBrokerException;
import com.alibaba.rocketmq.client.exception.MQClientException;
import com.alibaba.rocketmq.client.producer.DefaultMQProducer;
import com.alibaba.rocketmq.common.message.Message;
import com.alibaba.rocketmq.remoting.exception.RemotingException;
import org.apache.commons.lang3.StringUtils;
import bean.DownloadParams;
import bean.FetchParams;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * Created by chen_ on 2017/5/6.
 */
public class JiandanSpider {
    //    private static Spider_MQ spider = new Spider_MQ();
    private static Spider spider = new Spider();
    public static void init() {

//        spider.setTask0(() -> {
//            DefaultMQProducer producer = new DefaultMQProducer(String.valueOf(0));
//            producer.setNamesrvAddr(spider.NAME_SRV_ADDR);
//            try {
//                producer.start();
//            } catch (MQClientException e) {
//                e.printStackTrace();
//            }
//            //*[@id="comment-3983468"]/div/div/div[2]/p/img
//            for (int i = 1; i < 38; i++) {
//                Message msg = new Message("Topic" + 0 /* Topic */,
//                        "TagA" /* Tag */,
//                        ("http://jandan.net/ooxx/page-" + i).getBytes() /* Message body */
//                );
//                try {
//                    producer.send(msg);
//                } catch (MQClientException | RemotingException e) {
//                    e.printStackTrace();
//                } catch (MQBrokerException e) {
//                    e.printStackTrace();
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//            }
//        });
        LinkedBlockingQueue<String> q = new LinkedBlockingQueue<>(Constant.CAPACITY);
        for (int i = 0; i < 38; i++) {
            q.offer("http://jandan.net/ooxx/page-" + i);
        }
        spider.setQueues0(q);
    }

    public static void main(String[] args) {
        init();
        spider.chain(url -> FetchParams.builder().url("" + url).xpath("//*[@class=text]/p/img/@src").flag(FetchParams.Flag.simple).build()).print()
                .download(
                        url -> DownloadParams.builder().filename("./煎蛋/" + StringUtils.substringAfterLast("" + url, "/")).url("http:" + url).build()
                ).end();
    }

//    private static Spider spider = new Spider();
//
//    public static void init() {
//        ArrayBlockingQueue<String> q = new ArrayBlockingQueue<>(1000, true);
//        for (int i = 1; i < 38; i++) {
//            q.offer("http://jandan.net/ooxx/page-"+i);
//        }
//        spider.setQueues0(q);
//    }
//
//    public static void main(String[] args) {
//        init();
//        spider.chain(url -> FetchParams.builder().url("" + url).xpath("//*[@class=text]/p/img/@src").flag(FetchParams.Flag.simple).build()).download(
//                url-> DownloadParams.builder().filename("./煎蛋/"+StringUtils.substringAfterLast(""+url, "/")).url("http:" + url).build()
//        );
//    }
}